/*
 * ProxyServer.h
 *
 *  Created on: Apr 22, 2012
 *      Author: shaohong
 */

#ifndef PROXYSERVER_H_
#define PROXYSERVER_H_

#include <stdbool.h>
#include <string>
#include <vector>

using namespace std;

#include "Hop.h"
#include "HttpResponse.h"
#include "HttpRequest.h"

struct ProxServerConfig {
	vector<Hop*> nextHopServers;	// the next hop servers

	string servingDomain;	// the domain that this proxy servers as the reverse proxy

	string cacheDir; // the directory to save cached content

	uint32_t proxyPort;		// the port that proxy server should bind to

};

class ProxyServer {
public:
	ProxyServer(struct ProxServerConfig serverConfig);
	virtual ~ProxyServer();

	// run the proxy server
	void run();

    uint32_t getServerPort() const;

    string getCacheDir() const;

    void service(int connfd);	// serve the given request

	// gracefully shutdown the webserver;
	void gracefulShutDown();

	// Whether the given resource can be found in cache
	bool hasResourceInCache(string resourceName);

	void sendResponse(HttpResponse * response, int connfd);

	// check if the domain in the request matches the reverse proxy domain
	bool matchesDomain(HttpRequest * httpRequest);

	struct ProxServerConfig serverConfig;

    int getThreadNumber();

    void increamentThreadNumber();

    void decrementThreadNumber();

private:

	int threadNumber;	// the number of running thread
	pthread_mutex_t threadNumberMutex; // the mutex to access running thread number

	bool serverShutingDown;

};

#endif /* PROXYSERVER_H_ */
